<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/model/UnitService.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Entity.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Unit.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/exceptions/DataBaseException.php';

class UnitDAOImpl extends UnitDAO {

	public function create(Entity $unit) {
/*		$sql = 'INSERT INTO `unit` (`name`) ';
		$sql .= 'VALUES ("'.$unit->getName().'")';
		try {
			$this->query($sql);
			$unit->setIdentity($this->identity());
			return $unit;
		} catch(DataBaseException $exception) {
			return null;
		}
*/

		$name = $unit->getName();
		
		$stmt = $this->dbh->connection->prepare('INSERT INTO unit (name) VALUES (?)');
		$stmt->bindParam(1, $name);
		
		try {
			$this->dbh->query($stmt);
			$unit->setIdentity($this->dbh->identity());
			return $unit;
		} catch (PDOException $exception) {
			return null;
		}
		
	}

	public function read($identity) {
/*		$units = $this->build('`id`='.((int)$identity));
		if(count($units) == 1) {
			return $units[0];
		} else {
			return null;
		}
*/

		$id = ((int)$identity);
		
		$stmt = $this->dbh->connection->prepare('id=?');
		$stmt->bindParam(1, $id);
		
		$posts = $this->build($stmt);
		if(count($units) == 1) {
			return $units[0];
		} else {
			return null;
		}
		
	}

	public function update(Entity $unit) {
/*		$sql = 'UPDATE `unit` ';
		$sql .= 'SET `name`="'.$unit->getName().'" ';
		$sql .= 'WHERE `id`='.$unit->getIdentity();
		try {
			$this->query($sql);
			return $unit;
		} catch(DataBaseException $exception) {
			return null;
		}
*/

		$name = $unit->getName();
		$id = $unit->getIdentity();
		
		$stmt = $this->dbh->connection->prepare('UPDATE unit SET name=? WHERE id=?');
		$stmt->bindParam(1, $name);
		$stmt->bindParam(2, $id);
		
		try {
			$this->dbh->query($stmt);
			$unit->setIdentity($this->dbh->identity());
			return $unit;
		} catch (PDOException $exception) {
			return null;
		}
		
	}

	public function delete($identity) {
/*		$sql = 'DELETE FROM `unit` ';
		$sql .= 'WHERE `id`='.((int)$identity);
		try {
			$this->query($sql);
			return true;
		} catch(DataBaseException $exception) {
			return false;
		}
*/

		$id = (int)($identity);
		
		$stmt = $this->dbh->connection->prepare('DELETE FROM unit WHERE id=?');
		$stmt->bindParam(1, $id);
		
		try {
			$this->dbh->query($stmt);
			return true;
		} catch (PDOException $exception) {
			return false;
		}
				
	}

	// выборка с условием $condition
	private function build($condition) {
/*		$units = array();
		$sql = 'SELECT `id`,`name` ';
		$sql .= 'FROM `unit`';
		if($condition !== null) {
			$sql .= ' WHERE '.$condition;
		}
		$this->query($sql);
		while($row = $this->row()) {
			$unit = new Unit();
			$unit->setIdentity($row['id']);
			$unit->setName($row['name']);
			$units[] = $unit;
		}
		$this->close();
		return $units;
*/

		$sql = 'SELECT id, name FROM unit';
		if($condition !== null) {
			$sql = 'SELECT id, name FROM unit WHERE ?';
		}
		
		$stmt = $this->dbh->connection->prepare($sql);
		if($condition !== null) {
			$stmt->bindParam(1, $condition);
		}
		
		$this->dbh->query($stmt);
		$units = array();
		while($row = $this->dbh->row()) {
			$unit = new Unit();
			$unit->setIdentity($row['id']);
			$unit->setName($row['name']);
			$units[] = $unit;
		}
		$this->dbh->close();
		return $units;
		
	}
	
	// получить список единиц измерения
	public function getAll(){
/*		$units = array();
		$sql = 'SELECT `id`,`name` ';
		$sql .= 'FROM `unit`';
		$this->query($sql);
		while($row = $this->row()) {
			$unit = new Unit();
			$unit->setIdentity($row['id']);
			$unit->setName($row['name']);
			$units[] = $unit;
		}
		$this->close();
		return $units;
*/

		$stmt = $this->dbh->connection->prepare('SELECT id, name FROM unit');
		$this->dbh->query($stmt);
		$units = array();
		while($row = $this->dbh->row()) {
			$unit = new Unit();
			$unit->setIdentity($row['id']);
			$unit->setName($row['name']);
			$units[] = $unit;
		}
		$this->dbh->close();
		return $units;
							
	}
}
?>